classdef CPatch< handle & matlab.mixin.Copyable
    properties
%         grounds = {};
%         detects = {};
        grounds = [];
        detects = [];
    end
    properties(Constant = true)
        M = containers.Map({'seed', 'seed_1', 'partial'}, {0, 1, 2});
    end
    methods(Static = true)
        function class = getClassId(label)
            class = CPatch.M(label);
        end
    end
    methods
        function obj = CPatch(fileGroundTrue, fileDetectRes, iou)
            if nargin == 0
                obj.detects = [];
                obj.grounds = [];
                return;
            end
            
            fid = fopen(fileGroundTrue);
            data = textscan(fid,'%s %f %f %f %f');
            fclose(fid);
            
            nums = length(data{1});
            grounds = zeros(nums, 5);
            
            for i = 1 : nums
                label = CPatch.getClassId(data{1}{i});
                grounds(i, :) = [label, data{2}(i), data{3}(i), data{4}(i), data{5}(i)];
            end
            
            obj.grounds = grounds;
            
            
            fid = fopen(fileDetectRes);
            data = textscan(fid,'%s %f %f %f %f %f');
            fclose(fid);
            nums = length(data{1});
% %             detects = zeros(nums, 6);
            detects = zeros(nums, 8);%%增加最后一列作为边缘判断
            
            for i = 1 : nums
                label = CPatch.getClassId(data{1}{i});                
                
                detects(i, 1:6) = [label, data{2}(i), data{3}(i), data{4}(i), data{5}(i), data{6}(i)];
            end
            ins = detects(:, 2) >= iou;
            detects = detects(ins, :);
            obj.detects = detects;
        end
        
% % %         %%抑制partial，没用
% % %         function supressPartials(obj, ratio)
% % %             detects = obj.detects;
% % %             
% % %             ins = detects(:, 1) == 2;
% % %             detects(ins, 2) = detects(ins, 2) * ratio;            
% % %         end
        
        %% 寻找边缘元素
        function findEdgeBox(obj)
           wSize = CImage.wsize - 1;
           for i = 1 : size(obj.detects, 1)
               box = obj.detects(i, 3:6);
               
               %% 发现 seed(2)_(4,4)的检测结果有-1坐标的，这里就要用不等号
               if box(1) <= 0 || box(2) <= 0 || box(3) >= wSize || box(4) >= wSize
                   obj.detects(i, 7) = 1;
               end
           end
        end
        
        %%抑制边缘元素
        function supressEdges(obj, ratio)
            obj.detects(:, 8) = obj.detects(:, 2);%%保存原来的prob
            
            ins = obj.detects(:, 7) == 1;
            obj.detects(ins, 2) = obj.detects(ins, 2) * ratio;            
        end
    end
end